package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import util.DataBaseConnection;
import bean.Marca;
import dao.Entidad;

public class MySQLMarcaDAO implements Entidad<Marca>{
	
	@Override
	public Marca insert(Marca t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO marca(nomb_marc)";
			sql=sql + " values(?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setString(1, t.getNombre());
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setId_marca(id);
			}		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public int update(Marca t) {
		Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE marca SET nomb_marc = ?,";
			sql = sql +	" WHERE id_marc = ?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setObject(1, t.getNombre());
			ps.setObject(2, t.getId_marca());
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;	
	}

	@Override
	public int delete(Marca t) {
		Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM marca WHERE id_marc=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_marca());
			rows=ps.executeUpdate();
			System.out.println(rows);
				
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public Marca findById(Marca t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM marca WHERE id_marc=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_marca());
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				t.setId_marca(rs.getInt("id_marc"));
				t.setNombre(rs.getString("nomb_marc"));
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public List<Marca> listAll() {
		List<Marca> marca = new ArrayList<Marca>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="Select * from marca";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Marca t = new Marca();
				t.setId_marca(rs.getInt("id_marc"));
				t.setNombre(rs.getString("nomb_marc"));
				marca.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return marca;
	}

	@Override
	public List<Marca> listAllCustom(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}

}
